﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TeamTasker.Client.Presentation.Controls.SearchInTreeControl;

namespace TeamTasker.Client.Presentation.Controls.TaskTreeControl
{
    /// <summary>
    /// Interaction logic for TaskTreeControl.xaml
    /// </summary>
    public partial class TaskTreeControl : UserControl
    {
        private ObservableCollection<NodeModel> _InternalItemsSource = new ObservableCollection<NodeModel>();

        public TaskTreeControl()
        {
            InitializeComponent();
            ItemsSourceChanged += OnItemsSourceEventRaisedChanged;
            ItemsList.ItemsSource = _InternalItemsSource;
        }

        public IEnumerable ItemsSource
        {
            get { return (IEnumerable)GetValue(ItemsSourceProperty); }
            set
            {
                SetValue(ItemsSourceProperty, value);
            }
        }

        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(IEnumerable), typeof(TaskTreeControl), new PropertyMetadata(OnItemsSourceChanged));

        public static readonly RoutedEvent ItemsSourceChangedEvent = EventManager.RegisterRoutedEvent("ItemsSourceChanged", RoutingStrategy.Direct,
            typeof(RoutedEventHandler), typeof(TaskTreeControl));

        public event RoutedEventHandler ItemsSourceChanged
        {
            add { AddHandler(ItemsSourceChangedEvent, value); }
            remove { RemoveHandler(ItemsSourceChangedEvent, value); }
        }

        private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var element = d as UIElement;
            if (element != null)
            {
                element.RaiseEvent(new RoutedEventArgs(ItemsSourceChangedEvent));
            }
        }

        private void OnItemsSourceEventRaisedChanged(object sender, RoutedEventArgs e)
        {
            if (ItemsSource != null)
            {
                _InternalItemsSource.Clear();
                foreach (NodeModel item in ItemsSource)
                {
                    _InternalItemsSource.Add(item);
                }
            }
        }

        private void OnItemExpanded(object sender, RoutedEventArgs e)
        {
            var expander = e.OriginalSource as Expander;
            if (expander != null)
            {
                var nodeToExpand = expander.DataContext as NodeModel;
                if (nodeToExpand != null)
                {
                    var i = _InternalItemsSource.IndexOf(nodeToExpand);
                    foreach (var nodeModel in nodeToExpand.SubModels)
                    {
                        _InternalItemsSource.Insert(++i, nodeModel);
                    }
                }
            }
        }

        private void OnItemCollapsed(object sender, RoutedEventArgs e)
        {
            var expander = e.OriginalSource as Expander;
            if (expander != null)
            {
                var nodeToExpand = expander.DataContext as NodeModel;
                if (nodeToExpand != null)
                {
                    foreach (var nodeModel in nodeToExpand.SubModels)
                    {
                        _InternalItemsSource.Remove(nodeModel);
                    }
                }
            }
        }
    }
}
